<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>连续差相同的数字</title>
</head>
<body>
  <script>
    function fn(n, k) {
      const ans = [];
      const dfs = (i, cur) => {
        if (i === n) {
          return ans.push(cur.join('') * 1);
        }
        const pre = cur[cur.length - 1];
        if (pre + k >= 0 && pre + k <= 9) {
          dfs(i + 1, [...cur, pre + k]);
        }
        // pre - k !== pre + k 是做一个去重
        if (pre - k !== pre + k && pre - k >= 0 && pre - k <= 9) {
          dfs(i + 1, [...cur, pre - k]);
        }
      }
      for (let num = 1; num <= 9; num++) {
        dfs(1, [num]);
      }
      return ans
    }
    console.log(fn(3, 7))
  </script>
</body>
</html>